const fp = require('lodash/fp');
const fs = require('fs');
// const process = require('process');

class IO {
  static of(x) {
    return new IO(() => x);
  }

  constructor(fn) {
    this._value = fn;
  }

  map(fn) {
    return new IO(fp.flowRight(fn, this._value));
  }

  join() {
    return this._value();
  }

  flatMap(fn) {
    return this.map(fn).join();
  }
}

// let r = IO.of(process).map(p => p.execPath);
// console.log(r._value());

let readFile = (fileName) => {
  return new IO(() => {
    return fs.readFileSync(fileName, 'utf-8');
  });
}

let print = (x) => {
  return new IO(() => {
    console.log(x);
    return x;
  })
}

let cat = fp.flowRight(print, readFile);
// cat('package.json')._value()._value();

readFile('package.json').flatMap(print).join();
